0

Brain_Tumor.ipynb

No Headings

The table of contents shows headings in notebooks and supported files.

Skip to Main
Jupyter

Brain_Tumor

Last Checkpoint: 2 minutes ago
  • File
  • Edit
  • View
  • Run
  • Kernel
  • Settings
  • Help
JupyterLab
Python [conda env:base] *
Kernel status: Idle Executed 2 cellsElapsed time: 1982 seconds
image/svg+xml
    [3]:
    !pip install tensorflow
    Collecting tensorflow
    [7]:
    Selection deleted
    import os
    import numpy as np
    import tensorflow as tf
    import matplotlib.pyplot as plt
    import seaborn as sns
    from tensorflow.keras.models import Model
    from tensorflow.keras.layers import GlobalAveragePooling2D, Dense, Dropout, Input
    from tensorflow.keras.applications import EfficientNetB0
    from tensorflow.keras.preprocessing.image import ImageDataGenerator
    from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint
    from sklearn.utils.class_weight import compute_class_weight
    from sklearn.metrics import classification_report, confusion_matrix
    from sklearn.utils.multiclass import unique_labels

    # === CONFIG ===
    DATA_DIR = "C:/Users/Rahila/Downloads/brain_tumor_dataset"
    IMG_SIZE = (224, 224)
    BATCH_SIZE = 32
    NUM_CLASSES = 4
    MODEL_PATH = "efficientnet_model.keras"
    CLASS_NAMES = ['Glioma', 'Meningioma', 'No Tumor', 'Pituitary']

    # === DATA LOADERS ===
    train_gen = ImageDataGenerator(
    rescale=1./255,
    rotation_range=20,
    width_shift_range=0.1,
    height_shift_range=0.1,
    zoom_range=0.1,
    horizontal_flip=True,
    vertical_flip=True
    ).flow_from_directory(
    os.path.join(DATA_DIR, 'train'),
    target_size=IMG_SIZE,
    batch_size=BATCH_SIZE,
    class_mode='sparse'
    )

    val_gen = ImageDataGenerator(rescale=1./255).flow_from_directory(
    os.path.join(DATA_DIR, 'val'),
    target_size=IMG_SIZE,
    batch_size=BATCH_SIZE,
    class_mode='sparse'
    )

    test_gen = ImageDataGenerator(rescale=1./255).flow_from_directory(
    os.path.join(DATA_DIR, 'test'),
    target_size=IMG_SIZE,
    batch_size=1,
    class_mode='sparse',
    shuffle=False
    )

    # === CLASS WEIGHTS ===
    labels = train_gen.classes
    class_weights = compute_class_weight(
    class_weight='balanced',
    classes=np.unique(labels),
    y=labels
    )
    class_weights = dict(enumerate(class_weights))

    # === MODEL ===
    base_model = EfficientNetB0(include_top=False, weights='imagenet', input_tensor=Input(shape=(224, 224, 3)))
    base_model.trainable = False
    x = base_model.output
    x = GlobalAveragePooling2D()(x)
    x = Dropout(0.5)(x)
    x = Dense(128, activation='relu')(x)
    x = Dropout(0.3)(x)
    output = Dense(NUM_CLASSES, activation='softmax')(x)
    model = Model(inputs=base_model.input, outputs=output)

    model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
    Found 1695 images belonging to 1 classes.
    Found 502 images belonging to 1 classes.
    Found 246 images belonging to 1 classes.
    
    C:\Users\Rahila\anaconda3\Lib\site-packages\keras\src\trainers\data_adapters\py_dataset_adapter.py:121: UserWarning: Your `PyDataset` class should call `super().__init__(**kwargs)` in its constructor. `**kwargs` can include `workers`, `use_multiprocessing`, `max_queue_size`. Do not pass these arguments to `fit()`, as they will be ignored.
      self._warn_if_super_not_called()
    
    Epoch 1/30
    53/53 ━━━━━━━━━━━━━━━━━━━━ 99s 2s/step - accuracy: 0.9070 - loss: 0.2737 - val_accuracy: 1.0000 - val_loss: 1.5559e-06
    Epoch 2/30
    53/53 ━━━━━━━━━━━━━━━━━━━━ 75s 1s/step - accuracy: 1.0000 - loss: 2.3420e-05 - val_accuracy: 1.0000 - val_loss: 1.3177e-06
    Epoch 3/30
    53/53 ━━━━━━━━━━━━━━━━━━━━ 76s 1s/step - accuracy: 1.0000 - loss: 1.6340e-05 - val_accuracy: 1.0000 - val_loss: 1.1949e-06
    Epoch 4/30
    53/53 ━━━━━━━━━━━━━━━━━━━━ 76s 1s/step - accuracy: 1.0000 - loss: 3.4486e-05 - val_accuracy: 1.0000 - val_loss: 1.1814e-06
    Epoch 5/30
    53/53 ━━━━━━━━━━━━━━━━━━━━ 75s 1s/step - accuracy: 1.0000 - loss: 2.5730e-05 - val_accuracy: 1.0000 - val_loss: 9.5819e-07
    Epoch 6/30
    53/53 ━━━━━━━━━━━━━━━━━━━━ 76s 1s/step - accuracy: 1.0000 - loss: 1.8743e-05 - val_accuracy: 1.0000 - val_loss: 8.3684e-07
    Epoch 7/30
    53/53 ━━━━━━━━━━━━━━━━━━━━ 76s 1s/step - accuracy: 1.0000 - loss: 2.0960e-05 - val_accuracy: 1.0000 - val_loss: 6.2169e-07
    Epoch 8/30
    53/53 ━━━━━━━━━━━━━━━━━━━━ 82s 1s/step - accuracy: 1.0000 - loss: 1.3034e-05 - val_accuracy: 1.0000 - val_loss: 5.9605e-07
    Epoch 9/30
    53/53 ━━━━━━━━━━━━━━━━━━━━ 77s 1s/step - accuracy: 1.0000 - loss: 1.1171e-05 - val_accuracy: 1.0000 - val_loss: 4.8942e-07
    Epoch 10/30
    53/53 ━━━━━━━━━━━━━━━━━━━━ 76s 1s/step - accuracy: 1.0000 - loss: 1.0529e-05 - val_accuracy: 1.0000 - val_loss: 4.7684e-07
    Epoch 11/30
    53/53 ━━━━━━━━━━━━━━━━━━━━ 76s 1s/step - accuracy: 1.0000 - loss: 5.5299e-06 - val_accuracy: 1.0000 - val_loss: 3.6285e-07
    Epoch 12/30
    53/53 ━━━━━━━━━━━━━━━━━━━━ 77s 1s/step - accuracy: 1.0000 - loss: 5.4624e-06 - val_accuracy: 1.0000 - val_loss: 3.5763e-07
    Epoch 13/30
    53/53 ━━━━━━━━━━━━━━━━━━━━ 75s 1s/step - accuracy: 1.0000 - loss: 5.0444e-06 - val_accuracy: 1.0000 - val_loss: 2.4079e-07
    Epoch 14/30
    53/53 ━━━━━━━━━━━━━━━━━━━━ 75s 1s/step - accuracy: 1.0000 - loss: 1.2188e-05 - val_accuracy: 1.0000 - val_loss: 2.3842e-07
    Epoch 15/30
    53/53 ━━━━━━━━━━━━━━━━━━━━ 75s 1s/step - accuracy: 1.0000 - loss: 7.1924e-06 - val_accuracy: 1.0000 - val_loss: 2.3842e-07
    Epoch 16/30
    53/53 ━━━━━━━━━━━━━━━━━━━━ 74s 1s/step - accuracy: 1.0000 - loss: 8.6971e-06 - val_accuracy: 1.0000 - val_loss: 2.3842e-07
    Epoch 17/30
    53/53 ━━━━━━━━━━━━━━━━━━━━ 163s 3s/step - accuracy: 1.0000 - loss: 3.8749e-06 - val_accuracy: 1.0000 - val_loss: 2.3842e-07
    Epoch 18/30
    53/53 ━━━━━━━━━━━━━━━━━━━━ 77s 1s/step - accuracy: 1.0000 - loss: 5.4178e-06 - val_accuracy: 1.0000 - val_loss: 1.2040e-07
    Epoch 19/30
    53/53 ━━━━━━━━━━━━━━━━━━━━ 78s 1s/step - accuracy: 1.0000 - loss: 1.8848e-06 - val_accuracy: 1.0000 - val_loss: 1.1398e-07
    Epoch 20/30
    53/53 ━━━━━━━━━━━━━━━━━━━━ 77s 1s/step - accuracy: 1.0000 - loss: 7.7386e-06 - val_accuracy: 1.0000 - val_loss: 0.0000e+00
    Epoch 21/30
    53/53 ━━━━━━━━━━━━━━━━━━━━ 75s 1s/step - accuracy: 1.0000 - loss: 3.5452e-06 - val_accuracy: 1.0000 - val_loss: 0.0000e+00
    Epoch 22/30
    53/53 ━━━━━━━━━━━━━━━━━━━━ 165s 3s/step - accuracy: 1.0000 - loss: 2.6283e-06 - val_accuracy: 1.0000 - val_loss: 0.0000e+00
    Epoch 23/30
    53/53 ━━━━━━━━━━━━━━━━━━━━ 75s 1s/step - accuracy: 1.0000 - loss: 2.1228e-06 - val_accuracy: 1.0000 - val_loss: 0.0000e+00
    Epoch 24/30
    53/53 ━━━━━━━━━━━━━━━━━━━━ 76s 1s/step - accuracy: 1.0000 - loss: 1.5144e-06 - val_accuracy: 1.0000 - val_loss: 0.0000e+00
    Epoch 25/30
    53/53 ━━━━━━━━━━━━━━━━━━━━ 75s 1s/step - accuracy: 1.0000 - loss: 4.4546e-05 - val_accuracy: 1.0000 - val_loss: 0.0000e+00
    246/246 ━━━━━━━━━━━━━━━━━━━━ 24s 68ms/step - accuracy: 1.0000 - loss: 0.0000e+00
    ✅ Test Accuracy: 1.0000
    246/246 ━━━━━━━━━━━━━━━━━━━━ 19s 65ms/step
                  precision    recall  f1-score   support
    
          Glioma       1.00      1.00      1.00       246
    
        accuracy                           1.00       246
       macro avg       1.00      1.00      1.00       246
    weighted avg       1.00      1.00      1.00       246
    
    
    C:\Users\Rahila\anaconda3\Lib\site-packages\sklearn\metrics\_classification.py:409: UserWarning: A single label was found in 'y_true' and 'y_pred'. For the confusion matrix to have the correct shape, use the 'labels' parameter to pass all known labels.
      warnings.warn(
    
    [ ]:

    Common Tools
    No metadata.
    Advanced Tools
    No metadata.
    Anaconda Assistant
    AI-powered coding, insights and debugging in your notebooks.
    To enable the following extensions, create an account or sign in.
    • Anaconda Assistant
      4.1.0
    • Coming soon!
    • Data Catalogs
    • Panel Deployments
    • Sharing
    Already have an account? Sign In
    For more information, read our Anaconda Assistant documentation.
    Alt+[
    Alt+]
    Alt+End
    • Assistant
    • Open Anaconda Assistant
      Ctrl+Shift+A
    • Console
    • Change Kernel…
    • Clear Console Cells
    • Close and Shut Down…
    • Insert Line Break
    • Interrupt Kernel
    • New Console
    • Restart Kernel…
    • Run Cell (forced)
    • Run Cell (unforced)
    • Show All Kernel Activity
    • Display Languages
    • English
      English
    • File Operations
    • Autosave Documents
    • Download
      Download the file to your computer
    • Reload Notebook from Disk
      Reload contents from disk
    • Revert Notebook to Checkpoint…
      Revert contents to previous checkpoint
    • Save Notebook
      Save and create checkpoint
      Ctrl+S
    • Save Notebook As…
      Save with new path
      Ctrl+Shift+S
    • Trust HTML File
      Whether the HTML file is trusted. Trusting the file allows scripts to run in it, which may result in security risks. Only enable for files you trust.
    • Help
    • About Jupyter Notebook
    • Jupyter Reference
    • JupyterLab FAQ
    • JupyterLab Reference
    • Launch Jupyter Notebook File Browser
    • Markdown Reference
    • Show Keyboard Shortcuts…
      Show relevant keyboard shortcuts for the current active widget
      Ctrl+Shift+H
    • Image Viewer
    • Flip image horizontally
      H
    • Flip image vertically
      V
    • Invert Colors
      I
    • Reset Image
      0
    • Rotate Clockwise
      ]
    • Rotate Counterclockwise
      [
    • Zoom In
      =
    • Zoom Out
      -
    • Kernel Operations
    • Shut Down All Kernels…
    • Main Area
    • Close All Other Tabs
    • Close Tab
      Alt+W
    • Close Tabs to Right
    • End Search
      Esc
    • Find Next
      Ctrl+G
    • Find Previous
      Ctrl+Shift+G
    • Find…
      Ctrl+F
    • Log Out
      Log out of Jupyter Notebook
    • Search in Selection
      Alt+L
    • Shut Down
      Shut down Jupyter Notebook
    • Mode
    • Toggle Zen Mode
    • Notebook Cell Operations
    • Change to Code Cell Type
      Y
    • Change to Heading 1
      1
    • Change to Heading 2
      2
    • Change to Heading 3
      3
    • Change to Heading 4
      4
    • Change to Heading 5
      5
    • Change to Heading 6
      6
    • Change to Markdown Cell Type
      M
    • Change to Raw Cell Type
      R
    • Clear Cell Output
      Clear outputs for the selected cells
    • Collapse All Code
    • Collapse All Outputs
    • Collapse Selected Code
    • Collapse Selected Outputs
    • Copy Cell
      Copy this cell
      C
    • Cut Cell
      Cut this cell
      X
    • Delete Cell
      Delete this cell
      D, D
    • Disable Scrolling for Outputs
    • Enable Scrolling for Outputs
    • Expand All Code
    • Expand All Outputs
    • Expand Selected Code
    • Expand Selected Outputs
    • Extend Selection Above
      Shift+K
    • Extend Selection Below
      Shift+J
    • Extend Selection to Bottom
      Shift+End
    • Extend Selection to Top
      Shift+Home
    • Insert Cell Above
      Insert a cell above
      A
    • Insert Cell Below
      Insert a cell below
      B
    • Insert Heading Above Current Heading
      Shift+A
    • Insert Heading Below Current Heading
      Shift+B
    • Merge Cell Above
      Ctrl+Backspace
    • Merge Cell Below
      Ctrl+Shift+M
    • Merge Selected Cells
      Shift+M
    • Move Cell Down
      Move this cell down
      Ctrl+Shift+Down
    • Move Cell Up
      Move this cell up
      Ctrl+Shift+Up
    • Paste Cell Above
      Paste this cell from the clipboard
    • Paste Cell and Replace
    • Paste Cell Below
      Paste this cell from the clipboard
      V
    • Redo Cell Operation
      Shift+Z
    • Render Side-by-Side
      Shift+R
    • Run Selected Cell
      Run this cell and advance
      Shift+Enter
    • Run Selected Cell and Do not Advance
      Ctrl+Enter
    • Run Selected Cell and Insert Below
      Alt+Enter
    • Run Selected Text or Current Line in Console
    • Select Cell Above
      K
    • Select Cell Below
      J
    • Select Heading Above or Collapse Heading
      Left
    • Select Heading Below or Expand Heading
      Right
    • Set side-by-side ratio
    • Split Cell
      Ctrl+Shift+-
    • Undo Cell Operation
      Z
    • Notebook Operations
    • Access Next Kernel History Entry
      Alt+Down
    • Access Previous Kernel History Entry
      Alt+Up
    • Change Kernel…
    • Clear Outputs of All Cells
      Clear all outputs of all cells
    • Close and Shut Down Notebook…
    • Collapse All Headings
      Ctrl+Shift+Left
    • Deselect All Cells
    • Edit Notebook Metadata
    • Enter Command Mode
      Ctrl+M
    • Enter Edit Mode
      Enter
    • Expand All Headings
      Ctrl+Shift+Right
    • Interrupt Kernel
      Interrupt the kernel
    • New Console for Notebook
    • New Notebook
      Create a new notebook
    • Open with Panel in New Browser Tab
    • Preview Notebook with Panel
    • Reconnect to Kernel
    • Render All Markdown Cells
    • Restart Kernel and Clear Outputs of All Cells…
      Restart the kernel and clear all outputs of all cells
    • Restart Kernel and Debug…
      Restart Kernel and Debug…
    • Restart Kernel and Run All Cells…
      Restart the kernel and run all cells
    • Restart Kernel and Run up to Selected Cell…
    • Restart Kernel…
      Restart the kernel
    • Run All Above Selected Cell
    • Run All Cells
      Run all cells
    • Run Selected Cell and All Below
    • Save and Export Notebook: Asciidoc
    • Save and Export Notebook: Executable Script
    • Save and Export Notebook: HTML
    • Save and Export Notebook: LaTeX
    • Save and Export Notebook: Markdown
    • Save and Export Notebook: PDF
    • Save and Export Notebook: Qtpdf
    • Save and Export Notebook: Qtpng
    • Save and Export Notebook: ReStructured Text
    • Save and Export Notebook: Reveal.js Slides
    • Save and Export Notebook: Webpdf
    • Select All Cells
      Ctrl+A
    • Show Line Numbers
    • Toggle Collapse Notebook Heading
    • Trust Notebook
    • Other
    • Open in JupyterLab
      JupyterLab
    • Plugin Manager
    • Advanced Plugin Manager
    • Terminal
    • Decrease Terminal Font Size
    • Increase Terminal Font Size
    • New Terminal
      Start a new terminal session
    • Refresh Terminal
      Refresh the current terminal session
    • Use Terminal Theme: Dark
      Set the terminal theme
    • Use Terminal Theme: Inherit
      Set the terminal theme
    • Use Terminal Theme: Light
      Set the terminal theme
    • Text Editor
    • Decrease Font Size
    • Increase Font Size
    • New Markdown File
      Create a new markdown file
    • New Python File
      Create a new Python file
    • New Text File
      Create a new text file
    • Spaces: 1
    • Spaces: 2
    • Spaces: 4
    • Spaces: 4
    • Spaces: 8
    • Theme
    • Decrease Code Font Size
    • Decrease Content Font Size
    • Decrease UI Font Size
    • Increase Code Font Size
    • Increase Content Font Size
    • Increase UI Font Size
    • Set Preferred Dark Theme: JupyterLab Dark
    • Set Preferred Dark Theme: JupyterLab Dark High Contrast
    • Set Preferred Dark Theme: JupyterLab Light
    • Set Preferred Light Theme: JupyterLab Dark
    • Set Preferred Light Theme: JupyterLab Dark High Contrast
    • Set Preferred Light Theme: JupyterLab Light
    • Synchronize Styling Theme with System Settings
    • Theme Scrollbars
    • Use Theme: JupyterLab Dark
    • Use Theme: JupyterLab Dark High Contrast
    • Use Theme: JupyterLab Light
    • View
    • File Browser
    • Open JupyterLab
    • Show Anaconda Assistant
      Show Show Anaconda Assistant in the right sidebar
    • Show Header
    • Show Notebook Tools
      Show Show Notebook Tools in the right sidebar
    • Show Table of Contents
      Show Show Table of Contents in the left sidebar